import 'dart:convert';

import 'package:boss/provider/binding_provider.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/binding/binding_page.dart';
import 'package:boss/ui/page/binding/model/reviewPayInfoStatus.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class BindingReviewBrowser extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: SingleChildScrollView(
            padding: EdgeInsets.only(
              top: 52.w,
            ),
            child: Consumer<BindingProvider>(
              builder: (context, bindingProvide, _) {
                String wxError;

                if (bindingProvide.wechatHistory.applyStatus == ReviewPayInfoStatus.REJECTED) {
                  var error = json.decode(bindingProvide.wechatHistory.auditDetail);
                  wxError = error['audit_detail'][0]['reject_reason'];
                }

                return Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    _ReviewPanel(
                      title: '微信特约商户',
                      status: bindingProvide.wechatHistory?.applyStatus ?? 0,
                      error: wxError,
                      sigendUrl: bindingProvide.wechatHistory.signUrl,
                    ),
                    SizedBox(
                      height: 120.w,
                    ),
                    _ReviewPanel(
                      title: '支付宝特约商户',
                      status: bindingProvide.alipayHistory?.applyStatus ?? 0,
                      error: bindingProvide.alipayHistory?.auditDetail,
                      sigendUrl: bindingProvide.alipayHistory?.signUrl,
                    ),
                  ],
                );
              },
            ),
          ),
        ),
        Consumer<BindingProvider>(
          builder: (context, bindingProvider, _) {
            if (bindingProvider.wechatHistory.applyStatus == ReviewPayInfoStatus.REJECTED &&
                bindingProvider.alipayHistory.applyStatus == ReviewPayInfoStatus.REJECTED) {
              return FixedBottomBtn(
                title: '编 辑',
                onTap: () {
                  Navigator.popAndPushNamed(context, BindingPage.pageName);
                },
              );
            }

            return Container();
          },
        ),
      ],
    );
  }
}

class _TitleStatus extends StatelessWidget {
  final String title;
  final String status;

  const _TitleStatus({this.title, this.status});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          Text(
            title,
            style: TextStyleConstant().normal_1_36,
          ),
          Container(
            margin: EdgeInsets.only(
              top: 15.w,
            ),
            child: Text(
              '（$status）',
              style: TextStyleConstant().normal_3_28,
            ),
          ),
        ],
      ),
    );
  }
}

class _ReviewPanel extends StatelessWidget {
  final String title;
  final int status;
  final String error;
  final String sigendUrl;

  const _ReviewPanel({
    Key key,
    this.title,
    this.status,
    this.error,
    this.sigendUrl,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<Widget> children = [];

    children.add(_TitleStatus(
      title: title,
      status: ReviewPayInfoStatus.int2String(status),
    ));

    if (status == ReviewPayInfoStatus.REJECTED) {
      children.add(Container(
        padding: EdgeInsets.symmetric(
          vertical: 15.w,
        ),
        child: Text(
          error,
          style: TextStyleConstant().normal_1_28,
        ),
      ));
    }

    if (status == ReviewPayInfoStatus.TO_BE_SIGNED) {
      children.add(Container(
        margin: EdgeInsets.only(
          top: 30.w,
        ),
        child: BaseImage(
          sigendUrl,
          width: 312.w,
          height: 312.w,
        ),
      ));
    }

    return Column(
      children: children,
    );
  }
}
